<?php
/******************************************************************************
    NuCMS Framework, for the quick deployment of applications developed in
    php.
    Copyright (C) 2009  Matthew General

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
/**
 * 
 * @author mgeneral
 *
 */
class Debug {
	/**
	 * Debug Constants for Debug Levels
	 * All Debugging Information Possible
	 * Displays all information possible
	 * @var unknown_type
	 */
	const DEBUG_ALL = 5;
	/**
	 * Debug Constants for Debug Levels
	 * Display warnings only 
	 * @var unknown_type
	 */
	const DEBUG_WARNING = 4;
	/**
	 * Debug Constants for Debug Levels
	 * Display no debug information (default)
	 * @var unknown_type
	 */
	const DEBUG_NONE = 0;
	
	/**
	 * Application Debug Level
	 * @var unknown_type
	 */
	protected $_debugLevel;
	
	/**
	 * Application Settings
	 * @var unknown_type
	 */
	public $_settings;

	
	/**
	 * Constructor, sets default debug level
	 * @return unknown_type
	 */
	public function __construct($file = 'Config/config.ini') {
        if (!$this->_settings = parse_ini_file($file, TRUE)) throw new Error('Unable to open ' . $file . '.');
        unset($this->_settings["database"]);//security
        
		$this->setDebugLevel(constant($this->_settings["all"]["debugLevel"]));
		$this->debugMessage(self::DEBUG_ALL,print_r($this->_settings,true));
	}
	
	/**
	 * Sets debug level for the application
	 * @param $debugLevel
	 * @return unknown_type
	 */
	public function setDebugLevel($debugLevel=DEBUG_NONE) {
		if($debugLevel != self::DEBUG_ALL && $debugLevel != self::DEBUG_WARNING && $debugLevel != self::DEBUG_NONE) {
			trigger_error("Debug Level is not a level that actually exists. Should be DEBUG_ALL, DEBUG_WARNING or DEBUG_NONE (default), given ".$debugLevel,E_USER_ERROR);
		}
		$this->_debugLevel=$debugLevel;
		$this->debugMessage(self::DEBUG_ALL,"Debug Level Set to ".$debugLevel);
	}
	
	/**
	 * Debug Message Output
	 * @param $level the debug level for when to output
	 * @param $message the message the debug text should conveigh
	 * @return unknown_type
	 */
	public function debugMessage($level,$message) {
		switch($level) {
			case self::DEBUG_ALL:
				$levelText = "ALL LEVEL";
				break;
			case self::DEBUG_WARNING:
				$levelText = "WARNING LEVEL";
				break;
			case self::DEBUG_NONE:
				$levelText = "NONE LEVEL";
				break;
			default:
				break;
		}
		if($this->_debugLevel >= $level) {
			echo "<pre>Debug Level: ".$levelText." ".$level." of ".$this->_debugLevel."<br />Class :".get_class($this)."<br />Message: ".$message.PHP_EOL."<br /><br /></pre>";
		}
	}
	
	/**
	 * Debugging magic function, tells you when a method is called on a class that has yet to be added
	 * and what arguments were sent
	 * @param $name
	 * @param $args
	 * @return unknown_type
	 */
	public function __call($name,$args) {
		$this->debugMessage(self::DEBUG_WARNING,"call to ".$name." with args ".(implode(", ",$args))." in ".get_class($this));
	}
	
	/**
	 * Debugging magic function, tells you when a static method is called on a class that has yet to be added
	 * and what arguments were sent
	 * @param $name
	 * @param $args
	 * @return unknown_type
	 */
	public static function __callStatic($name, $args) {
		//Causes a problem on php 5.3.3, temporary fix
		//$this->debugMessage(self::DEBUG_WARNING,"static call to ".$name." with args ".(implode(", ",$args))." in ".get_class($this));
	}
	
	public function log($error) {
		if(!empty($this->_settings['all']['logging'])) {
			$this->debugMessage(self::DEBUG_WARNING,"Logging error ".$error);
			$fh=fopen($this->_settings['all']['logfile'],'a');
			fwrite($fh,$error);
			fclose($fh);
		}
		
	}
}
?>
